#include <allegro.h>
#include "general.h"
#include "collision.h"

int main()
{
SAMPLE *sound;
SAMPLE *effet;
BITMAP *Buffer;
BITMAP *Background;
messi m;
coins c1,c2,c3,c4,c5,c6;
obstacle o;
int i,j,compteur;


allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
Buffer = create_bitmap(800, 600);


install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
set_volume(255, 255);//Volume
 sound = load_sample("./son/hymne.wav");//chargement de la musique
 effet = load_sample("./son/chouta.wav");
play_sample(sound, 255, 128, 1000, 0);



Background = load_bitmap("/home/firastalbi/Images/background1.bmp", NULL);//chargement du background

//appel de la fonction chargememnt 
chargement (&m,&c1,&c2,&c3,&c4,&c5,&c6,&o);

c1.x = 200;//initialisation des variables
c1.y = 180;
c1.n=1;

c2.x = 300;
c2.y = 150;
c2.n=1;

c3.x = 400;
c3.y = 180;
c3.n=1;

c4.x = 500;
c4.y = 150;
c4.n=1;

c5.x = 600;
c5.y = 180;
c5.n=1;

c6.x = 700;
c6.y = 150;
c6.n=1;

m.x = 0;
m.y = 460;
m.direction=0;

o.x = 400;
o.y = 400;
o.n=1;

i=0;
compteur=0;
m.score=0;

while (!key[KEY_ESC]) {

//messi
int sprite1_x=m.x;
int sprite1_y=m.y;
int sprite1_w=m.img_right[0]->w;
int sprite1_h=m.img_right[0]->h;

//coins1
int sprite2_x=c1.x;
int sprite2_y=c1.y;
int sprite2_w=c1.img_coins->w;   
int sprite2_h=c1.img_coins->h;
 
//coins2
int sprite3_x=c2.x;
int sprite3_y=c2.y;
int sprite3_w=c2.img_coins->w;   
int sprite3_h=c2.img_coins->h;

//coins3
int sprite4_x=c3.x;
int sprite4_y=c3.y;
int sprite4_w=c3.img_coins->w;   
int sprite4_h=c3.img_coins->h;

//coins4
int sprite5_x=c4.x;
int sprite5_y=c4.y;
int sprite5_w=c4.img_coins->w;   
int sprite5_h=c4.img_coins->h;

//coins5
int sprite6_x=c5.x;
int sprite6_y=c5.y;
int sprite6_w=c5.img_coins->w;   
int sprite6_h=c5.img_coins->h;

//coins6
int sprite7_x=c6.x;
int sprite7_y=c6.y;
int sprite7_w=c6.img_coins->w;   
int sprite7_h=c6.img_coins->h;

//obstacle
int spriteo_x=o.x;
int spriteo_y=o.y;
int spriteo_w=o.img_obstacle->w;
int spriteo_h=o.img_obstacle->h;
		

//dessiner le background et l'obstacle
blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
//draw_sprite(Buffer,o.img_obstacle , o.x , o.y);

//utiliser cette fonction et dessiner dans le buffer et non dans le screen
textprintf_ex(Buffer, font, SCREEN_W - 110, 10, makecol(255, 255, 20),-1, "Score: %d", m.score);


//tester s'il ya collision
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite2_x,  sprite2_y,  sprite2_w,  sprite2_h) !=0 ) 
{c1.n=0;m.score+=10;c1.x=-10;c1.y=-10;play_sample(effet, 255, 128, 1000, 0);}//solution provvisoire à améliorer


if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite3_x,  sprite3_y,  sprite3_w,  sprite3_h) !=0 ) 
{c2.n=0;m.score+=10;c2.x=-10;c2.y=-10;play_sample(effet, 255, 128, 1000, 0);}//solution provvisoire à améliorer


if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite4_x,  sprite4_y,  sprite4_w,  sprite4_h) !=0 ) 
{c3.n=0;m.score+=10;c3.x=-10;c3.y=-10;play_sample(effet, 255, 128, 1000, 0);}//solution provvisoire à améliorer


if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite5_x,  sprite5_y,  sprite5_w,  sprite5_h) !=0 ) 
{c4.n=0;m.score+=10;c4.x=-10;c4.y=-10;play_sample(effet, 255, 128, 1000, 0);}//solution provvisoire à améliorer


if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite6_x,  sprite6_y,  sprite6_w,  sprite6_h) !=0 ) 
{c5.n=0;m.score+=10;c5.x=-10;c5.y=-10;play_sample(effet, 255, 128, 1000, 0);}//solution provvisoire à améliorer


if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite7_x,  sprite7_y,  sprite7_w,  sprite7_h) !=0 ) 
{c6.n=0;m.score+=10;c6.x=-10;c6.y=-10;play_sample(effet, 255, 128, 1000, 0);}//solution provvisoire à améliorer


if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  spriteo_x,  spriteo_y,  spriteo_w,  spriteo_h) !=0 )    o.n=0;  


//dessiner les coins s'il n'ya pas collision
if(c1.n==1){
draw_sprite(Buffer, c1.img_coins, c1.x ,c1.y);
}

if(c2.n==1){
draw_sprite(Buffer, c2.img_coins, c2.x ,c2.y);
}

if(c3.n==1){
draw_sprite(Buffer, c3.img_coins, c3.x ,c3.y);
}

if(c4.n==1){
draw_sprite(Buffer, c4.img_coins, c4.x ,c4.y);
}

if(c5.n==1){
draw_sprite(Buffer, c5.img_coins, c5.x ,c5.y);
}

if(c6.n==1){
draw_sprite(Buffer, c6.img_coins, c6.x ,c6.y);
}

//dessiner l'obstacle s'il n'ya pas collision
if(o.n==1){
draw_sprite(Buffer, o.img_obstacle, o.x ,o.y);
}else //Loosing
{
destroy_bitmap(Background);
Buffer = create_bitmap(800, 600);
textout_centre_ex(Buffer, font, "GAME OVER !!", SCREEN_W / 2, SCREEN_H / 2, makecol(255, 0,0), -1);
blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
allegro_message("VOTRE SCORE: %d", m.score) ;
return 1;
}
int tx[1]={750};
int ty[6]={480,150,450,300,250,2};

//bouger l'obstacle
do{
o.x--;
if (o.x == 1 ) 
{
o.x = tx[0];
o.y = ty[compteur];
compteur++;
}
if (compteur == 6){compteur=0;}
}while (o.x < 0 && compteur == -1);


///////////////////////////////////////

if (key[KEY_RIGHT] && m.x < SCREEN_W - m.img_right[0]->w) {
i++;
m.x++;
m.direction=0;
}

if (key[KEY_LEFT] && m.x >0) {
i--;
m.x--;
m.direction=1;
}

if (key[KEY_UP] && m.y > 0) {
m.y--;
}

if (key[KEY_DOWN] && m.y <SCREEN_H - m.img_right[0]->h) {
m.y++;
}

//dessiner messi 

draw_sprite(Buffer, m.img_right[i % 3], m.x, m.y);


////////////////////////////////////////

blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
clear_bitmap(Buffer);
		

if(i==1000){
i=0;
}


}//fin while


for(j=0;j<3;j++){
destroy_bitmap(m.img_right[j]);
//destroy_bitmap(m.img_left[j]);
}
destroy_bitmap(Buffer);
destroy_bitmap(c1.img_coins);
destroy_bitmap(c2.img_coins);
destroy_bitmap(c3.img_coins);
destroy_bitmap(c4.img_coins);
destroy_bitmap(c5.img_coins);
destroy_bitmap(c6.img_coins);
destroy_bitmap(o.img_obstacle);


return 0;

}//fin main





